package com.ysd.lis.service.callNum.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.callNum.CallBusiness;
import com.ysd.lis.entity.callNum.CallWindow;
import com.ysd.lis.mapper.callNum.CallBusinessMapper;
import com.ysd.lis.mapper.callNum.CallWindowMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.callNum.CallNumParam;
import com.ysd.lis.service.callNum.CallNumHomeService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-08-29
 */
@Service
public class CallNumHomeServiceImpl implements CallNumHomeService {

    @Autowired
    CallWindowMapper callWindowMapper;
    @Override
    public Result getCallNumAgency(CommonPageDto pageParam) {
        Map<String,Object> returnMap = new HashMap<>();

        List<CallWindow> windowList = callWindowMapper
                .selectList(new LambdaQueryWrapper<CallWindow>().eq(CallWindow::getDelFlag, 0).orderByAsc(CallWindow::getSeq));
        returnMap.put("allWindowCnt",windowList.size());
        //查询未启用的数量
        Integer noWindowCnt = Math.toIntExact(windowList.stream().filter(a -> a.getIsEnable() == 0).count());
        returnMap.put("noWindowCnt",noWindowCnt);


        return Result.succ(1, "查询成功", returnMap);

    }

    @Autowired
    private CallBusinessMapper callBusinessMapper;

    @Override
    public Result getCallNumAgency2(CommonPageDto pageParam) {
        Map<String,Object> returnMap = new HashMap<>();
        //今日采样量排行
        MPJLambdaWrapper<CallBusiness> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(CallBusiness.class)
                .eq(CallBusiness::getDelFlag,0)
                .ge(CallBusiness::getReqTime, DateUtil.beginOfDay(new Date()).toTimestamp())
                .le(CallBusiness::getReqTime, DateUtil.endOfDay(new Date()).toTimestamp());

        List<CallBusiness> callBusinessList = callBusinessMapper.selectList(lambdaQueryWrapper);
        //根据窗口分组获取每个窗口的数量
        if(ToolsUtils.isNotEmpty(callBusinessList)){
            List<Map> callBusinessCntlist = new ArrayList<>();
            Map<List<Object>, List<CallBusiness>> callBusinessMap = callBusinessList.stream().collect(Collectors.groupingBy(s -> Arrays.asList(s.getWinCode(), s.getWinName())));
            for (List<Object> clist : callBusinessMap.keySet()) {
                Map<String,Object> callBusinessCntMap = new HashMap<>();
                callBusinessCntMap.put("code",clist.get(0));
                callBusinessCntMap.put("value",clist.get(0));
                callBusinessCntMap.put("name",clist.get(1));
                callBusinessCntMap.put("label",clist.get(1));
                callBusinessCntMap.put("cnt",callBusinessMap.get(clist).size());
                callBusinessCntMap.put("avgTime",callBusinessMap.get(clist).stream().filter(a -> Objects.nonNull(a.getAvgTime())).mapToInt(CallBusiness::getAvgTime).average().orElse(0));

                callBusinessCntlist.add(callBusinessCntMap);
            }
            //根据cnt排序
            callBusinessCntlist.sort((o1, o2) -> {
                Integer cnt1 = (Integer) o1.get("cnt");
                Integer cnt2 = (Integer) o2.get("cnt");
                return cnt1.compareTo(cnt2); // 升序排列
            });
            returnMap.put("callBusinessCntlist",callBusinessCntlist);
        }
        return Result.succ(1, "查询成功", returnMap);
    }

    @Override
    public Result getCallBusinessTjfx(CallNumParam param) {
        String paramMonth = param.getMonth();
        Date today = DateUtil.parse(paramMonth, "yyyy-MM");
        Map<String,Object> returnMap = new HashMap<>();

        //今日采样量排行
        MPJLambdaWrapper<CallBusiness> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(CallBusiness.class)
                .eq(CallBusiness::getDelFlag,0)
                .ge(CallBusiness::getReqTime, DateUtil.beginOfMonth(today).toTimestamp())
                .le(CallBusiness::getReqTime, DateUtil.endOfMonth(today).toTimestamp());

        List<CallBusiness> callBusinessList = callBusinessMapper.selectList(lambdaQueryWrapper);

        //根据窗口分组获取每个窗口的数量
        List<Map> callBusinessCntlist = new ArrayList<>();
        if(ToolsUtils.isNotEmpty(callBusinessList)){
            Map<List<Object>, List<CallBusiness>> callBusinessMap = callBusinessList.stream().collect(Collectors.groupingBy(s -> Arrays.asList(s.getWinCode(), s.getWinName())));
            for (List<Object> clist : callBusinessMap.keySet()) {
                Map<String,Object> callBusinessCntMap = new HashMap<>();
                callBusinessCntMap.put("code",clist.get(0));
                callBusinessCntMap.put("value",clist.get(0));
                callBusinessCntMap.put("name",clist.get(1));
                callBusinessCntMap.put("label",clist.get(1));
                callBusinessCntMap.put("cnt",callBusinessMap.get(clist).size());
                //保留两位小数
                callBusinessCntMap.put("avgTime",new DecimalFormat("0.00").format(callBusinessMap.get(clist).stream().filter(a -> Objects.nonNull(a.getAvgTime())).mapToInt(CallBusiness::getAvgTime).average().orElse(0)));
                callBusinessCntlist.add(callBusinessCntMap);
            }
            //根据cnt排序
            callBusinessCntlist.sort((o1, o2) -> {
                Integer cnt1 = (Integer) o1.get("cnt");
                Integer cnt2 = (Integer) o2.get("cnt");
                return cnt1.compareTo(cnt2); // 升序排列
            });
        }
        returnMap.put("callBusinessCntlist",callBusinessCntlist);
        return Result.succ(1, "查询成功", returnMap);
    }
}
